<template>
	<view class="set-password">

		<!-- 密码输入区域 -->
		<view class="password-section">
			<view class="input-group">
				<input 
					class="password-input" 
					:type="showPassword ? 'text' : 'password'" 
					placeholder="请输入新密码" 
					v-model="newPassword"
					@input="onPasswordInput"
				/>
				<view class="eye-btn" @click="togglePasswordVisibility">
					<text class="iconfont" :class="showPassword ? 'icon-eye-open' : 'icon-eye-close'"></text>
				</view>
			</view>
			
			<view class="input-group">
				<input 
					class="password-input" 
					:type="showConfirmPassword ? 'text' : 'password'" 
					placeholder="请确认新密码" 
					v-model="confirmPassword"
					@input="onConfirmPasswordInput"
				/>
				<view class="eye-btn" @click="toggleConfirmPasswordVisibility">
					<text class="iconfont" :class="showConfirmPassword ? 'icon-eye-open' : 'icon-eye-close'"></text>
				</view>
			</view>
		</view>

		<!-- 密码强度提示 -->
		<view class="password-strength" v-if="newPassword">
			<text class="strength-text">密码强度：</text>
			<view class="strength-bar">
				<view class="strength-fill" :class="strengthClass"></view>
			</view>
			<text class="strength-label">{{ strengthLabel }}</text>
		</view>

		<!-- 确认按钮 -->
		<view class="confirm-btn-section">
			<button 
				class="confirm-btn" 
				:class="{ 'active': canConfirm }"
				:disabled="!canConfirm"
				@click="handleConfirm"
			>
				确认修改
			</button>
		</view>

		<!-- 提示消息 -->
		<view v-if="showToast" class="toast-message" :class="toastType">
			{{ toastMessage }}
		</view>
	</view>
</template>

<script>
	import { checkPasswordStrength, getPasswordStrengthLabel, getPasswordStrengthClass } from '@/utils/verification.js';
	
	export default {
		data() {
			return {
				newPassword: '', // 新密码
				confirmPassword: '', // 确认密码
				showPassword: false, // 是否显示密码
				showConfirmPassword: false, // 是否显示确认密码
				showToast: false, // 是否显示提示
				toastMessage: '', // 提示消息
				toastType: 'error', // 提示类型
				canConfirm: false // 是否可以确认
			}
		},
		computed: {
			// 密码强度计算
			passwordStrength() {
				return checkPasswordStrength(this.newPassword);
			},
			
			// 强度样式类
			strengthClass() {
				return getPasswordStrengthClass(this.passwordStrength);
			},
			
			// 强度标签
			strengthLabel() {
				return getPasswordStrengthLabel(this.passwordStrength);
			}
		},
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack();
			},
			
			// 密码输入处理
			onPasswordInput() {
				this.checkCanConfirm();
			},
			
			// 确认密码输入处理
			onConfirmPasswordInput() {
				this.checkCanConfirm();
			},
			
			// 检查是否可以确认
			checkCanConfirm() {
				this.canConfirm = this.newPassword.length >= 6 && 
								this.newPassword === this.confirmPassword;
			},
			
			// 切换密码显示
			togglePasswordVisibility() {
				this.showPassword = !this.showPassword;
			},
			
			// 切换确认密码显示
			toggleConfirmPasswordVisibility() {
				this.showConfirmPassword = !this.showConfirmPassword;
			},
			
			// 显示提示消息
			showToastMessage(message, type = 'error') {
				this.toastMessage = message;
				this.toastType = type;
				this.showToast = true;
				
				setTimeout(() => {
					this.showToast = false;
				}, 3000);
			},
			
			// 处理确认
			handleConfirm() {
				if (!this.canConfirm) {
					return;
				}
				
				// 验证密码强度
				if (this.passwordStrength < 3) {
					this.showToastMessage('密码强度不够，请设置更复杂的密码', 'error');
					return;
				}
				
				// 模拟修改密码
				this.simulateChangePassword();
			},
			
			// 模拟修改密码
			simulateChangePassword() {
				// 显示加载状态
				uni.showLoading({
					title: '修改中...'
				});
				
				// 模拟网络请求
				setTimeout(() => {
					uni.hideLoading();
					this.showToastMessage('密码修改成功', 'success');
					
					// 延迟跳转到登录页面
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/index/index'
						});
					}, 1500);
				}, 2000);
			}
		}
	}
</script>

<style scoped>
.set-password {
	min-height: 100vh;
	background-color: #f5f5f5;
}

/* 头部样式 */
.header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 20rpx 30rpx;
	background-color: #fff;
	border-bottom: 1rpx solid #eee;
}

.header-left {
	width: 60rpx;
	height: 60rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.header-title {
	font-size: 36rpx;
	font-weight: 500;
	color: #333;
}

.header-right {
	display: flex;
	align-items: center;
	gap: 20rpx;
}

.header-right .iconfont {
	font-size: 32rpx;
	color: #666;
}

/* 密码输入区域 */
.password-section {
	padding: 40rpx 30rpx;
	background-color: #fff;
}

.input-group {
	display: flex;
	align-items: center;
	margin-bottom: 30rpx;
	border: 1rpx solid #ddd;
	border-radius: 8rpx;
	background-color: #fff;
}

.password-input {
	flex: 1;
	height: 80rpx;
	padding: 0 20rpx;
	font-size: 32rpx;
	border: none;
	background-color: transparent;
}

.eye-btn {
	width: 80rpx;
	height: 80rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.eye-btn .iconfont {
	font-size: 32rpx;
	color: #666;
}

/* 密码强度 */
.password-strength {
	padding: 20rpx 30rpx;
	background-color: #fff;
	border-top: 1rpx solid #eee;
}

.strength-text {
	font-size: 28rpx;
	color: #666;
	margin-bottom: 10rpx;
}

.strength-bar {
	height: 8rpx;
	background-color: #eee;
	border-radius: 4rpx;
	margin-bottom: 10rpx;
	overflow: hidden;
}

.strength-fill {
	height: 100%;
	transition: all 0.3s ease;
}

.strength-fill.weak {
	width: 33%;
	background-color: #ff4757;
}

.strength-fill.medium {
	width: 66%;
	background-color: #ffa502;
}

.strength-fill.strong {
	width: 100%;
	background-color: #2ed573;
}

.strength-label {
	font-size: 24rpx;
	color: #666;
}

/* 确认按钮 */
.confirm-btn-section {
	padding: 60rpx 30rpx;
}

.confirm-btn {
	width: 100%;
	height: 88rpx;
	border-radius: 8rpx;
	font-size: 32rpx;
	color: #fff;
	background-color: #ccc;
	border: none;
}

.confirm-btn.active {
	background-color: #007aff;
}

/* 提示消息 */
.toast-message {
	position: fixed;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	padding: 20rpx 40rpx;
	border-radius: 8rpx;
	font-size: 28rpx;
	color: #fff;
	z-index: 999;
}

.toast-message.error {
	background-color: rgba(0, 0, 0, 0.8);
}

.toast-message.success {
	background-color: rgba(0, 0, 0, 0.8);
}

/* 图标字体 */
/* .iconfont {
	font-family: "iconfont";
}

.icon-arrow-left::before {
	content: "\e6db";
}

.icon-more::before {
	content: "\e6de";
}

.icon-minus::before {
	content: "\e6df";
}

.icon-circle::before {
	content: "\e6e0";
} */

/* .icon-eye-open:before {
  content: "\e78f";
}

.icon-eye-close:before {
  content: "\e6ac";
} */
</style> 